#include "threadpool.h"
#include <iostream>

using ulong = unsigned long long;


ulong sum(ulong begin,ulong end)
{
	static int taskid = 1;
	std::cout << "task run begin using pid=" << std::this_thread::get_id() << "   taskid = "<<taskid<<std::endl;
	ulong res = 0;
	for (ulong i = begin; i <= end; i++)
	{
		res+= i;
	}
	std::this_thread::sleep_for(std::chrono::seconds(3));
	std::cout << "task run end tid=" << std::this_thread::get_id() << "   taskid = " << taskid << std::endl;
	taskid++;
	return res;
}


int main(int argc, char** argv)
{
	ThreadPool pool;
	//pool.setMode(PoolMode::MODE_CACHED);
	pool.start(4);

	std::future<ulong> res1 = pool.submitTask(sum, 1, 100000000);
	std::future<ulong> res2 = pool.submitTask(sum, 100000001, 200000000);
	std::future<ulong> res3 = pool.submitTask(sum, 200000001, 300000000);
	res1.wait();
	res2.wait();
	res3.wait();
	if (res1.valid() && res2.valid() && res3.valid())
	{
		ulong sum = res1.get() + res2.get() + res3.get();
		std::cout << "result = " << sum << std::endl;
	}
	else
	{
		std::cout << "result error" << std::endl;
	}




	getchar();


	return 0;
}